//
//  WJImageView.m
//  WJBSApp
//
//  Created by dllo on 16/7/21.
//  Copyright © 2016年 王景伟. All rights reserved.
//
// 大的Frame在Cell里面写
#import "WJImageView.h"
#import "DALabeledCircularProgressView.h"
#import "WJEssAndNewModel.h"
#import "WJShowPictureVC.h"
#import "WJDataManager.h"


@interface WJImageView ()

@property (nonatomic,strong) WJBaseImageView *huiSeDeImage;
// 上面写"点击查看全图"
@property (nonatomic,strong) UILabel *huiseLable;
//  加载的圆形宽
@property (nonatomic,strong) DALabeledCircularProgressView *progressView;

@end

@implementation WJImageView

-(void)dealloc
{
    [_model release];
    [_huiSeDeImage release];
    [_huiseLable release];
    [_progressView release];
    [super dealloc];
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createView];
        
    }
    return self;
}


- (void)createView
{
    
    // 取消自动收缩功能
    
    self.autoresizingMask = UIViewAutoresizingNone;
    
    // 开启用户交互

    self.userInteractionEnabled = YES;

    
    // 这个是圆圈圈
    self.progressView = [[DALabeledCircularProgressView alloc] init];
    // 头变成圆的
    self.progressView.roundedCorners = YES;
    [self addSubview:self.progressView];
    self.progressView.hidden = YES;
     self.progressView.progressTintColor = [UIColor orangeColor];
    [_progressView release];
    
    self.huiSeDeImage = [[WJBaseImageView alloc]init];
    [self addSubview:self.huiSeDeImage];
    self.huiSeDeImage.backgroundColor = [UIColor clearColor];
    self.huiSeDeImage.image = [UIImage imageNamed:@"see-big-picture-background"];
    [_huiSeDeImage release];
    

    self.huiseLable = [[UILabel alloc]init];
    [self.huiSeDeImage addSubview:self.huiseLable];
    self.huiseLable.hidden = YES;
    self.huiseLable.text = @"点击查看全图";
    self.huiseLable.backgroundColor = [UIColor clearColor];
    self.huiseLable.textColor = [UIColor whiteColor];
    self.huiseLable.textAlignment = 1;
    [_huiseLable release];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [self addGestureRecognizer:tap];                 // 给视图添加手势  添加手指
    [tap release];

}


- (void)setModel:(WJEssAndNewModel *)model
{
    if (_model !=model) {
        [_model release];
        _model = [model retain];
    }
    
    
    [self sd_setImageWithURL:[NSURL URLWithString:self.model.imageModel.download_url[0]] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
        if ([[WJDataManager shareFMDB] isExistPicName:self.model.imageModel.download_url[0]]) {
            
            self.progressView.hidden = YES;
            
        }else{

        self.progressView.hidden = NO;
        
        self.huiSeDeImage.hidden = YES;

        CGFloat proges = 1.0 * receivedSize / expectedSize;
        
        // 显示文字
        NSString *text = [NSString stringWithFormat:@"%.0f%%", proges * 100];
        self.progressView.progressLabel.text = [text stringByReplacingOccurrencesOfString:@"-" withString:@""];
        //显示进度值
        [self.progressView setProgress:proges animated:NO];
        }
        
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        self.progressView.hidden = YES;
        
        if (![[WJDataManager shareFMDB] isExistPicName:self.model.imageModel.download_url[0]]) {
            
            [[WJDataManager shareFMDB] insertPicName:self.model.imageModel.download_url[0]];
            
        }

        NSInteger newHeight = self.model.imageModel.height * (WIDTH - 20) / self.model.imageModel.width;

        if (newHeight > HEIGHT) {
            
#pragma mark - 厉害
        //开启图形上下文
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);
        
        //将下载完的image对象绘制到图形上下文中
        
        CGFloat wight = self.bounds.size.width;
        CGFloat height = wight *image.size.height / self.bounds.size.width;
        
        [image drawInRect:CGRectMake(0, 0, wight, height)];
        
        //获得图片
        self.image = UIGraphicsGetImageFromCurrentImageContext();
        //结束图形上下文
        UIGraphicsEndImageContext();
        }

    }];
    
     NSInteger newHeight = self.model.imageModel.height * (WIDTH - 20) / self.model.imageModel.width;
    
    if (newHeight > HEIGHT) {
        
        self.huiSeDeImage.hidden = NO;
        self.huiseLable.hidden = NO;
        
    }else{
        
        self.huiSeDeImage.hidden = YES;
        self.huiseLable.hidden = YES;
    }
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
     NSInteger newHeight = self.model.imageModel.height * (WIDTH - 20) / self.model.imageModel.width;

    if (newHeight > HEIGHT) {
        
        self.huiSeDeImage.hidden = NO;
        self.huiseLable.hidden = NO; // 图片的Mode
        
        self.huiSeDeImage.frame = CGRectMake(0,  HEIGHT / 2 - 40, WIDTH - 20, 40);
        self.huiseLable.frame = CGRectMake(WIDTH / 2 - 110, 15 / 2, 200, 15);
        
    }else{
        
        self.huiSeDeImage.hidden = YES;
        self.huiseLable.hidden = YES;
    }

    self.progressView.frame = CGRectMake(self.bounds.size.width / 2 - 30, self.bounds.size.height / 2 - 30, 60, 60);
}

-(void)tapAction:(UITapGestureRecognizer *)tap{
    
    NSLog(@"点了了啊");
    
    WJShowPictureVC *vc = [[WJShowPictureVC alloc]init];
    
    vc.model = self.model;
    
    // 主屏显示
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:vc animated:YES completion:nil];
    
}





















@end
